package com.group.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.group.pojo.GEvaluate;
import com.group.pojo.GStudent;
import com.group.pojo.GUser;

/**
 * Servlet implementation class EvaluateInfoServlet
 */
@WebServlet("/EvaluateInfoServlet")
public class EvaluateInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public EvaluateInfoServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		
		//任务&评价信息（学生）
		if(action.equals("info")) {
			GEvaluate eva = new GEvaluate();
			GUser user = (GUser) request.getSession().getAttribute("SESSION_USER");
			GStudent student = new GStudent();
			int groupID = student.getGroupIDByID(user.getUserID());
			List<GEvaluate> evaList = eva.getEvaListByGroupID(groupID);
			request.setAttribute("evaList", evaList);
			request.getRequestDispatcher("tasks-ing.jsp").forward(request, response);
		}
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		
		//任务&评价信息(教师)
		if(action.equals("info")) {
			GEvaluate eva = new GEvaluate();
			String workID = request.getParameter("workID");
			List<GEvaluate> evaList = eva.getEvaListByWorkID(Integer.parseInt(workID));
			Gson gson = new Gson();
			String jsonObj = gson.toJson(evaList);
			response.setHeader("Cache-Control", "no-cache");// 去除缓存
			response.setContentType("application/json;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(jsonObj);
			out.flush();
			out.close();
		}
		else if(action.equals("eva")) {
			String[] evaluateID = request.getParameterValues("evaluateID");
			String[] evaluateContent = request.getParameterValues("evaluateContent");
			String[] evaluateDegree = request.getParameterValues("evaluateDegree");
			for(int i=0; i < evaluateID.length; i++) {
				GEvaluate eva = new GEvaluate(Integer.parseInt(evaluateID[i]), evaluateContent[i], evaluateDegree[i]);
				eva.updateEva(eva);
			}
			response.sendRedirect("WorkInfoServlet?action=info");
		}
	}

}
